#!/bin/bash

export CPUS=`grep -c processor /proc/cpuinfo`
TOP_DIR=`pwd`

function open_tap()
{
	if [ ! -d "/sys/class/net/tap0" ]; then
		sudo ip tuntap add dev tap0 mode tap
		sudo ip link set dev tap0 up
		sudo ip address add dev tap0 192.168.2.128/24
	fi
}

function build_stm32()
{
	scons
	if [ $? -eq 0 ]; then
	    echo "build stm32 success"
	else
	    echo "build stm32 failed"
	    exit 1
	fi
}

function run_stm32()
{
	open_tap
	qemu/qemu-system-arm -M stm32f429-st-disco  \
	-kernel $TOP_DIR/STM32F429.elf \
	-serial stdio \
	-show-cursor \
	-sd sdcard
}

function debug_stm32()
{
	open_tap
	qemu/qemu-system-arm -M stm32f429-st-disco  \
	-kernel $TOP_DIR/STM32F429.elf \
	-net nic -net tap,ifname=tap0,script=no,downscript=no \
	-serial stdio \
	-show-cursor \
	-sd sdcard \
	-S -s
}

function build_nuc980()
{
	cd $TOP_DIR/bsp/nuc980
	make   -j${CPUS}
	if [ $? -eq 0 ]; then
	    echo "build nuc980 success"
	else
	    echo "build nuc980 failed"
	    exit 1
	fi
	cd $TOP_DIR
}

function clean_nuc980()
{
	cd $TOP_DIR/bsp/nuc980
	make clean -j${CPUS}
	if [ $? -eq 0 ]; then
	    echo "clean nuc980 success"
	else
	    echo "clean nuc980 failed"
	    exit 1
	fi
	cd $TOP_DIR
}

function debug_nuc980()
{
	open_tap
	qemu/qemu-system-arm -M nuc980-evb  \
	-kernel $TOP_DIR/bsp/nuc980/build/nuc980.elf \
	-serial stdio \
	-show-cursor \
	-S -s
}

function run_nuc980()
{
	open_tap
	qemu/qemu-system-arm -M nuc980-evb  \
	-kernel $TOP_DIR/bsp/nuc980/build/nuc980.elf \
	-serial stdio \
	-show-cursor
}

function build_versatile()
{
	cd $TOP_DIR/bsp/versatile
	make   -j${CPUS}
	if [ $? -eq 0 ]; then
	    echo "build versatile success"
	else
	    echo "build versatile failed"
	    exit 1
	fi
	cd $TOP_DIR
}

function run_versatile()
{
	open_tap
	qemu/qemu-system-arm -M versatilepb  -m 128 \
	-kernel $TOP_DIR/bsp/versatile/image.bin \
	-net nic -net tap,ifname=tap0,script=no,downscript=no \
	-serial stdio \
	-show-cursor
}

function debug_versatile()
{
	open_tap
	qemu/qemu-system-arm -M versatilepb -m 128 \
	-kernel $TOP_DIR/bsp/versatile/image.elf \
	-net nic -net tap,ifname=tap0,script=no,downscript=no \
	-serial stdio \
	-show-cursor \
	-S -s
}

function usage()
{
	echo "Usage: build.sh [OPTIONS]"
	echo "Available options:"
	echo "all                -build stm32f429 freeRTOS"
	echo "stm32              -build stm32f429 freeRTOS"
	echo "stm32_run          -run stm32f429 freeRTOS"
	echo "stm32_debug        -debug stm32f429 freeRTOS"
	echo "versatile          -build versatile freeRTOS"
	echo "versatile_run      -run versatile freeRTOS"
	echo "versatile_debug    -debug versatile freeRTOS"
}

OPTIONS="${@:-all}"

for option in ${OPTIONS}; do
	echo "processing option: $option"
	case $option in
	all) build_stm32;;
        stm32)build_stm32;;
        stm32_run) run_stm32;;
        stm32_debug) debug_stm32;;
		nuc980) build_nuc980;;
		nuc980_clean) clean_nuc980;;
		nuc980_debug) debug_nuc980;;
		nuc980_run) run_nuc980;;
		versatile) build_versatile;;
        versatile_run) run_versatile;;
        versatile_debug) debug_versatile;;
	*) usage ;;
	esac
done